package com.bsj.power.pls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.PanelDTO;
import com.bsj.power.common.def.entity.Panel;
import com.bsj.power.common.def.vo.PanelVO;
import com.bsj.power.common.mapper.PanelMapper;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.common.util.ParamCheckUtil;
import com.bsj.power.pls.cache.PanelCache;
import com.bsj.power.pls.queue.service.impl.handle.QueueHumanFaceHandle;
import com.bsj.power.pls.service.PanelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author zhaoyang
 * @Date 2024/10/21 9:52
 */
@Slf4j
@Service
public class PanelServiceImpl implements PanelService {

    @Resource
    private PanelMapper panelMapper;

    @Override
    public JsonResult addPanel(PanelDTO panelDTO) {
        paramCheck(panelDTO);
        Panel panel = new Panel();
        BeanUtils.copyProperties(panelDTO, panel);
        boolean flag = panelMapper.insert(panel) > 0;
        if (flag) {
            String srcIndex = panel.getSrcIndex();
            PanelCache.addSrcIndex(srcIndex);
            PanelCache.putRelation(srcIndex, panel.getDeviceId());
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 参数校验
     *
     * @param panelDTO
     * @return void
     * @author ljx
     * @time 2024/10/30 17:06
     */
    private void paramCheck(PanelDTO panelDTO) {
        Integer id = panelDTO.getId();
        String srcIndex = panelDTO.getSrcIndex();
        String deviceId = panelDTO.getDeviceId();
        QueryWrapper<Panel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Panel::getSrcIndex, srcIndex);
        if (id != null) {
            queryWrapper.lambda().ne(Panel::getId, id);
        }
        if (panelMapper.exists(queryWrapper)) {
            throw new ApiException(JsonResultEnum.SRC_INDEX_EXIST);
        }
        queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Panel::getDeviceId, deviceId);
        if (id != null) {
            queryWrapper.lambda().ne(Panel::getId, id);
        }
        if (panelMapper.exists(queryWrapper)) {
            throw new ApiException(JsonResultEnum.DEVICE_NO_EXIST);
        }
    }

    @Override
    public JsonResult delPanel(Integer id) {
        Panel oldPanel = panelMapper.selectById(id);
        ParamCheckUtil.isNull(oldPanel);
        boolean flag = panelMapper.deleteById(id) > 0;
        if (flag) {
            String srcIndex = oldPanel.getSrcIndex();
            PanelCache.removeSrcIndex(srcIndex);
            PanelCache.removeRelation(srcIndex);
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @Override
    public JsonResult updatePanel(PanelDTO panelDTO) {
        Integer id = panelDTO.getId();
        ParamCheckUtil.isNull(id);
        Panel oldPanel = panelMapper.selectById(id);
        ParamCheckUtil.isNull(oldPanel);
        paramCheck(panelDTO);
        Panel panel = new Panel();
        BeanUtils.copyProperties(panelDTO, panel);
        int i = panelMapper.updateById(panel);
        if (i > 0) {
            String oldSrcIndex = oldPanel.getSrcIndex();
            //删除原先的
            PanelCache.removeSrcIndex(oldSrcIndex);
            PanelCache.removeRelation(oldSrcIndex);
            //添加新的
            String srcIndex = panel.getSrcIndex();
            PanelCache.addSrcIndex(srcIndex);
            PanelCache.putRelation(srcIndex, panel.getDeviceId());
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @Override
    public JsonResult listPanels(Integer pageNumber, Integer pageSize) {
        IPage<Panel> panelIPage = new Page<>(pageNumber, pageSize);
        QueryWrapper<Panel> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        IPage<Panel> pageResult = panelMapper.selectPage(panelIPage, wrapper);
        if (pageResult != null) {
            JsonResult jsonResult = JsonResult.success();
            List<Panel> records = pageResult.getRecords();
            List<PanelVO> vos = new ArrayList<>();
            records.forEach(panel -> {
                PanelVO vo = new PanelVO();
                BeanUtils.copyProperties(panel, vo);
                vo.setCreateTime(DateUtils.dateToStr(panel.getCreateTime()));
                vo.setUpdateTime(DateUtils.dateToStr(panel.getUpdateTime()));
                vos.add(vo);
            });
            jsonResult.setData(vos);
            jsonResult.setTotal(pageResult.getTotal());
            return jsonResult;
        }
        return JsonResult.fail();
    }
}
